package cn.pn.project.controller;

import cn.pn.project.client.commodity.CommodityFeign;
import cn.pn.project.client.user.AttentionFeign;
import cn.pn.project.common.DtoUtil;
import cn.pn.project.common.RedisUtils;
import cn.pn.project.module.commodity.AttentionModule;
import cn.pn.project.pojo.user.Attention;
import cn.pn.project.pojo.user.User;
import cn.pn.project.vo.CommodityVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/attention")
public class AttentionController {
    @Autowired
    private CommodityFeign commodityFeign;
    @Autowired
    private AttentionFeign attentionFeign;
    @Autowired
    private RedisUtils redisUtils;
    /**
     * 添加关注
     *
     * @param attention attentionId userId commodityId 必须有的参数
     * @return 结果
     * @throws RuntimeException
     */
    @PostMapping("/v/add") //不能关注重复的 关注的状态
    public Object addAttention(Attention attention) throws RuntimeException, SQLException {
        //判断参数是否符合要求
        if (attention == null)
            return DtoUtil.returnFail("没有参数", "1001");
        if (attention.getAttentionId() != null)
            return DtoUtil.returnFail("不能有参数:attentionId", "1001");
        if (attention.getUserId() == null || attention.getUserId().equals(0))
            return DtoUtil.returnFail("缺少参数:userId", "1001");
        if (attention.getCommodityId() == null || attention.getCommodityId().equals(0))
            return DtoUtil.returnFail("缺少参数:commodityId", "1001");
        Attention attention1=new Attention();
        attention1.setUserId(attention.getUserId());
        attention1.setCommodityId(attention.getCommodityId());
        attention1=attentionFeign.findAttention(attention1);
        if(attention1!=null)
            return DtoUtil.returnFail("每个账号对同一件商品只能关注一次", "1001");
        return attentionFeign.addAttention(attention) ? DtoUtil.returnSuccess("关注成功") : DtoUtil.returnFail("系统异常", "0000");

    }

    /**
     * 查询某用户指定状态的数据关系
     *
     * @param status 查询指定状态的关注
     * @return
     * @throws RuntimeException
     */
    @PostMapping("/v/find")
    public Object findAttentionByCommnuitStart(Integer status, HttpServletRequest request) throws RuntimeException {
        if (status == null || status.equals(0))
            return DtoUtil.returnSuccess("参数缺失", "1011");
        User user=null;
        try {
            user = (User) redisUtils.get(request.getHeader("token"));
        } catch (RuntimeException e) {
            System.out.println(request.getHeader("token"));
            return DtoUtil.returnFail("用户未登录","0001");
        }
        Attention attention = new Attention();
        attention.setUserId(user.getUserId());
        List<Attention> list = attentionFeign.findAttentionByUserIdAndStatus(attention);
        if (list == null || list.size() == 0)
            return DtoUtil.returnSuccess("暂无关注", "1012");
        //根据商品id 查询商品信息 留给东阳的接口： commnuitIds 尚品id 数组    商品状态 status  commodityIds
        //List<商品>CommnuitFeign.find商品ByCommnuitIds(String[] CommnuitIds,Integer status);
        List<Integer> commnuitIds = new ArrayList<Integer>();
        for (int i = 0; i < list.size(); i++) {
            commnuitIds.add(list.get(i).getCommodityId());
        }


        Map<String, Object> omap = new HashMap<String, Object>();

        omap.put("commodityIds", commnuitIds);
        omap.put("status", status);
        List<CommodityVo> commodityVoList = commodityFeign.getCommodityByCommodityIdAndCommodityStatus(omap);

        List<AttentionModule> attentionModules=new ArrayList<AttentionModule>();
        for(int i=0;i<list.size();i++){
            Attention item=list.get(i);
            AttentionModule attentionModule=new AttentionModule();
            attentionModule.setAttention(item);
            for(int j=0;j<commodityVoList.size();j++){
                CommodityVo cv=commodityVoList.get(j);
                if(cv.getCommodityId().equals(item.getCommodityId())){
                    attentionModule.setCommodityVo(cv);
                    break;
                }
            }
            attentionModules.add(attentionModule);
        }
        return DtoUtil.returnSuccess("成功获取关注",attentionModules);

    }

    /**
     * 取消关注
     * @param attention
     * @return
     * @throws RuntimeException
     */
    @PostMapping("/v/remove")
    public Object removeAttention(Attention attention,HttpServletRequest request) throws RuntimeException {
        if (attention == null)
            return DtoUtil.returnSuccess("参数缺失", "1011");
        if (attention.getAttentionId() == null || attention.getAttentionId().equals(0))
            return DtoUtil.returnSuccess("attentionId不能为空", "1011");
        if (attention.getCommodityId() == null || attention.getCommodityId().equals(0))
            return DtoUtil.returnSuccess("commodityId不能为空", "1011");
        String token =request.getHeader("token");
        User user=null;
        try {
            user= (User) redisUtils.get(token);
        }catch (Exception e){
            System.out.println(token);
            return DtoUtil.returnSuccess("用户未登录","0001");
        }
        attention.setUserId(user.getUserId());
        return attentionFeign.removeAttention(attention) ? DtoUtil.returnSuccess("成功取消关注") : DtoUtil.returnFail("系统异常", "0002");

    }
}
